package com.yx.yinshanjinniu.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.base.BaseActivity;


/**
 * Created by Administrator on 2016/8/4.
 */
public class TitleBar extends RelativeLayout implements View.OnClickListener {

    private ImageView iv_left;
    private ImageView iv_right;
    private TextView tv_title;
    private TextView tv_right;
    private TextView tv_left;
    private Context mContext;
    private View view;
    private View rl_panel;
    private View title_bottom_line;
    private TitleBarListener mListener;
    private boolean isTopPadding = true;
    private boolean showBottomLine = false;

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        view = LayoutInflater.from(mContext).inflate(R.layout.view_title_bar, this);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        findViews(view);
        initData(attrs);
    }

    private void findViews(View view) {
        iv_left =  view.findViewById(R.id.iv_left);
        iv_right =  view.findViewById(R.id.iv_right);
        tv_title =  view.findViewById(R.id.tv_title);
        tv_right =  view.findViewById(R.id.tv_right);
        tv_left =  view.findViewById(R.id.tv_left);
        rl_panel = view.findViewById(R.id.rl_panel);
        title_bottom_line = view.findViewById(R.id.title_bottom_line);
        iv_left.setOnClickListener(this);
        iv_right.setOnClickListener(this);
        tv_right.setOnClickListener(this);
        tv_left.setOnClickListener(this);
    }

    private void initData(AttributeSet attrs) {
        TypedArray arr = mContext.obtainStyledAttributes(attrs, R.styleable.TitleBar);
        Drawable img = arr.getDrawable(R.styleable.TitleBar_tbImage);
        Drawable imgRt = arr.getDrawable(R.styleable.TitleBar_tbImageRt);
        int bgColor = arr.getColor(R.styleable.TitleBar_tbBgColor, ContextCompat.getColor(mContext, R.color.white));
        String text = arr.getString(R.styleable.TitleBar_tbText);
        String rightText = arr.getString(R.styleable.TitleBar_tbRightText);
        String leftText = arr.getString(R.styleable.TitleBar_tbLeftText);
        int titleColor = arr.getColor(R.styleable.TitleBar_tbTextColor, 0);
        int rtColor = arr.getColor(R.styleable.TitleBar_tbRtTextColor, ContextCompat.getColor(mContext, R.color.color_gray_03));
        isTopPadding = arr.getBoolean(R.styleable.TitleBar_tbTopPadding, true);
        showBottomLine = arr.getBoolean(R.styleable.TitleBar_tbShowBottomLine, false);

        //设置状态栏padding
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && isTopPadding) {
            int height;
            int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                height = this.getResources().getDimensionPixelSize(resourceId);
            }else{
                height = (int) (getResources().getDisplayMetrics().density * 30 + 0.5f);
            }
            rl_panel.setPadding(0,height, 0, 0);
        }
        rl_panel.setBackgroundColor(bgColor);
        if (img != null) {
            iv_left.setImageDrawable(img);
            iv_left.setVisibility(View.VISIBLE);
        }
        if (imgRt != null) {
            iv_right.setImageDrawable(imgRt);
            iv_right.setVisibility(View.VISIBLE);
        }
        if (text != null) {
            tv_title.setText(text);
        }
        if (rightText != null) {
            tv_right.setText(rightText);
            tv_right.setVisibility(VISIBLE);
        }
        if (leftText != null) {
            tv_left.setText(leftText);
            tv_left.setVisibility(VISIBLE);
        }
        if (titleColor != 0) {
            tv_title.setTextColor(titleColor);
        }
        if (rtColor != 0) {
            tv_right.setTextColor(rtColor);
        }
        if(showBottomLine){
            title_bottom_line.setVisibility(VISIBLE);
        }else{
            title_bottom_line.setVisibility(GONE);
        }
        arr.recycle();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_left:
                if (mListener == null || mListener.onClickBack()) {
                    ((BaseActivity) mContext).backActivity();
                } else {
                    mListener.onLeftClick();
                }
                break;
            case R.id.tv_left:
                if (mListener != null) {
                    mListener.onLeftClick();
                }
                break;
            case R.id.tv_right:
                if (mListener != null) {
                    mListener.onRightClick();
                }
                break;
            case R.id.iv_right:
                if (mListener != null) {
                    mListener.onRightClick();
                }
                break;
        }
    }

    public void setTitle(String text) {
        tv_title.setText(text);
    }

    public void setTitleBarListener(TitleBarListener listener) {
        mListener = listener;
    }

    public void setImage(String url) {
//        Glide.with(mContext).load(url).apply(MyApplication.mImageOptions).into(iv_left);
    }

    public void setTitleBackgroud(int color){
        rl_panel.setBackgroundColor(ContextCompat.getColor(mContext,color));
    }

    public void setTitleRtImage(int resId){
        if(resId != 0){
            Drawable drawable = ContextCompat.getDrawable(getContext(),resId);;
            iv_right.setImageDrawable(drawable);
            iv_right.setVisibility(View.VISIBLE);
        }else{
            iv_right.setVisibility(GONE);
        }

    }

    public void setTitleRightVisible(int visible){
        tv_right.setVisibility(visible);
    }

    public interface TitleBarListener {
        void onLeftClick();

        void onRightClick();

        boolean onClickBack();
    }
}
